<?php

namespace App\System\Logic;

use Hyperf\Di\Annotation\Inject;
use App\System\Service\HeiRoleDeptService;
use Haozing\FastCore\Constants\ErrorCode;
use Haozing\FastCore\Exception\CommonException;

class HeiRoleDeptLogic
{
    #[Inject]
    protected HeiRoleDeptService $heiRoleDeptService;
    /**
     * 新增逻辑
     * @param array $params
     * @return array
     */
    public function add(array $params = []): array
    {
        // 参数处理
        $data = [
            'tenant_id' => $params['tenant_id'] ?? 0,

        ];
        // TODO 逻辑处理
        $data = $this->heiRoleDeptService->addOne($data);
        if (!$data) {
            throw new CommonException(ErrorCode::BAD_REQUEST,'新增失败');
        }
        return [];

    }

    /**
     * 更新逻辑
     * @param array $params
     * @return int
     */
    public function update(array $params = []): int
    {
        // 条件参数处理
        $where = [];
        // 更新数据参数处理
        $data = [];
        if (isset($params['tenant_id'])) {
            $data['tenant_id'] = $params['tenant_id'];
        }

        // TODO 逻辑处理
        $data = $this->heiRoleDeptService->updateByWhere($where,$data);
        if (!$data) {
            throw new CommonException(ErrorCode::BAD_REQUEST,'修改失败');
        }
        return $data;
    }

    /**
     * 删除逻辑
     * @param array $params
     * @return mixed
     */
    public function delete(array $params = []): array
    {
        // TODO 逻辑处理
        $data = $this->heiRoleDeptService->deleteByWhere($params);
        if (!$data) {
            throw new CommonException(ErrorCode::BAD_REQUEST,'删除失败');
        }
        return [];
    }

    /**
     * 详情逻辑
     * @param array $params
     * @return array
     */
    public function detail(array $params = []): array
    {
        // TODO 逻辑处理
        if (!isset($params['id']) ||!$params['id']) {
            throw new CommonException(ErrorCode::INVALID_PARAM,'参数错误');
        }
        $data = $this->heiRoleDeptService->getOneById($params['id'],['*']);
        if (!$data) {
            throw new CommonException(ErrorCode::NOT_FOUND,'资源不存在');
        }
        return $data;
    }

    /**
     * 列表查询逻辑
     * @param array $params
     * @return array
     */
    public function list(array $params = []): array
    {
        // 条件参数处理
        $where = [];
        // 其他参数处理
        $options = [];
        if (isset($params['tenant_id']) && $params['tenant_id']) {
            $where[] = ['tenant_id',$params['tenant_id']];
        }


        if (isset($params['pageSize']) && $params['pageSize']) {
            $options['pageSize'] = $params['pageSize'];
        }
        // TODO 逻辑处理
        $data = $this->heiRoleDeptService->getPage($where,['*'],$options);
        // TODO 数据聚合
        return [
            'total' => $data['total']??0,
            'list' => $data['data']??[],
        ];
    }
    /**
     * 列表查询[简单kv字段]
     * @param array $params
     * @return array
     */
    public function select(array $params = []): array
    {
        // 条件参数处理
        $where = [];
        // 其他参数处理
        $options = [];
        if (isset($params['tenant_id']) && $params['tenant_id']) {
            $where[] = ['tenant_id',$params['tenant_id']];
        }


        if (isset($params['pageSize']) && $params['pageSize']) {
            $options['pageSize'] = $params['pageSize'];
        }
        // TODO 逻辑处理
        $data = $this->heiRoleDeptService->getPage($where,['id','name'],$options);
        // TODO 数据聚合
        return [
            'total' => $data['total']??0,
            'list' => $data['data']??[],
        ];
    }
}